/* 
 * fastcgi.h --
 *
 *	Defines for the FastCGI protocol.
 *
 *
 * Copyright (c) 1995-1996 Open Market, Inc.
 *
 * See the file "LICENSE.TERMS" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * $Id: fastcgi.h,v 1.1.1.1 1997/09/16 15:36:32 stanleyg Exp $
 */

// Artem Kustikov: code refactored to be C++


#ifndef FASTCGI_DETAILS_HPP
#define FASTCGI_DETAILS_HPP

namespace fastcgi
{
	typedef unsigned char byte_type;

	// Listening socket file number
	const int listensock_fileno = 0;

	typedef struct {
		byte_type version;
		byte_type type;
		byte_type requestIdB1;
		byte_type requestIdB0;
		byte_type contentLengthB1;
		byte_type contentLengthB0;
		byte_type paddingLength;
		byte_type reserved;
	
	} header_struct;

	const size_t max_content_length  = 0xffff;

	// Number of bytes in a FCGI_Header.  Future versions of the protocol
	// will not reduce this number.
	const int header_len = 8;

	// Value for version component of FCGI Header
	const byte_type default_version = 1;

	// Value for requestId component of FCGI Header
	const byte_type null_request_id = 0;

	// Values for type component of FCGI Header
	enum fcgi_message_type
	{
		message_begin_request = 1,
		message_abort_request = 2,
		message_end_request   = 3,
		message_params        = 4,
		message_stdin         = 5,
		message_stdout        = 6,
		message_stderr        = 7,
		message_data          = 8,
		message_get_values    = 9,
		message_get_values_result = 10,
		message_unknown_type      = 11,
		message_maxtype  = message_unknown_type
	};
	

	typedef struct {
		byte_type roleB1;
		byte_type roleB0;
		byte_type flags;
		byte_type reserved[5];
	} begin_request_body_struct;

	typedef struct {
		header_struct header;
		begin_request_body_struct body;

	} begin_request_record_struct;

	// Mask for flags component of FCGI BeginRequestBody
	const byte_type flag_keep_conn = 1;

	// Values for role component of FCGI BeginRequestBody
	namespace roles
	{
		const byte_type responder = 1;
		const byte_type authorizer = 2;
		const byte_type filter = 3;

	}
	
	typedef struct {
		byte_type appStatusB3;
		byte_type appStatusB2;
		byte_type appStatusB1;
		byte_type appStatusB0;
		byte_type protocolStatus;
		byte_type reserved[3];
	
	} end_request_body_struct;

	// Values for protocolStatus component of FCGI EndRequestBody
	enum fcgi_protocol_status
	{
		status_request_complete	= 0,
		status_cant_mpx_conn	= 1,
		status_overloaded		= 2,
		status_unknown_role     = 3
	};

	typedef struct {
		header_struct header;
		end_request_body_struct body;
	
	} end_request_record_struct;


	// Variable names for FCGI_GET_VALUES / FCGI_GET_VALUES_RESULT records
	const char commmand_max_conns[]		= "FCGI_MAX_CONNS";
	const char commmand_max_reqs[]		= "FCGI_MAX_REQS";
	const char commmand_mpxs_conns[]	= "FCGI_MPXS_CONNS";

	typedef struct {
		byte_type type;    
		byte_type reserved[7];
	
	} unknown_type_body_struct;

	typedef struct {
		header_struct header;
		unknown_type_body_struct body;
	
	} unknown_type_record_struct;

}
#endif	// FASTCGI_DETAILS_HPP

